package com.mdinic.blogovi.service;

import java.io.StringWriter;

import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.VelocityException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import com.mdinic.blogovi.model.Email;
import com.mdinic.blogovi.model.EmailType;
import com.mdinic.blogovi.model.Person;

/**
 * Implementation of {@link EmailService}.
 * 
 * @author mdinic
 * 
 */
@Configuration
class EmailServiceImpl implements EmailService {

    private static final Logger LOG = Logger.getLogger(EmailServiceImpl.class);

    private static final String PERSON = "person";
    private static final String SERVER_URL = "server-url";

    @Autowired
    private EmailSender emailSender;

    @Value("${server.url}")
    private String serverUrl;

    @Value("${mail.noreply}")
    private String emailNoreply;

    @Override
    public void sendRegistrationEmail(Person person) {
        Email email = Email.findActiveEmailByType(EmailType.REGISTRATION);
        if (email == null) {
            LOG.error("Active Email template not found for REGISTRATION");
        } else {

            final VelocityContext context = new VelocityContext();

            context.put(PERSON, person);

            final String body = evaluate(context, email.getBody());
            emailSender.sendMail(person.getEmail(), emailNoreply, email.getSubject(), body);

        }
    }

    @Override
    public void sendForgotPasswordEmail(Person person) {
        Email email = Email.findActiveEmailByType(EmailType.FORGOT_PASSWORD);
        if (email == null) {
            LOG.error("Active Email template not found for FORGOT_PASSWORD");
        } else {

            final VelocityContext context = new VelocityContext();

            context.put(PERSON, person);

            final String body = evaluate(context, email.getBody());
            emailSender.sendMail(person.getEmail(), emailNoreply, email.getSubject(), body);
        }
    }

    @Override
    public void sendEmailChangeEmail(Person person) {
        Email email = Email.findActiveEmailByType(EmailType.NEW_EMAIL);
        if (email == null) {
            LOG.error("Active Email template not found for NEW_EMAIL");
        } else {

            final VelocityContext context = new VelocityContext();

            context.put(PERSON, person);

            final String body = evaluate(context, email.getBody());
            emailSender.sendMail(person.getEmail(), emailNoreply, email.getSubject(), body);
        }
    }

    private String evaluate(final VelocityContext context, final String templateValue) {
        context.put(SERVER_URL, serverUrl);
        try {
            final StringWriter stringWriter = new StringWriter();
            Velocity.evaluate(context, stringWriter, "", templateValue);
            return stringWriter.toString();
        } catch (final VelocityException e) {
            LOG.error("VelocityException occured", e);
            throw new IllegalStateException(e);
        }
    }

}